﻿using BlackList.Model;
using QWManage.DAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlackList.DAL
{
    public class YouZhaiDAL : SqlDAL
    {
        public YouZhaiDAL()
        {
            TableName = "youzhaiUsers";
        }
        public void UpdateQWID(string id, string qwid)
        {
            string sql = $"Update {TableName} set " +
                $"qwid=@qwid " +
                $"where id=@id";
            using (SqlConnection conn = new SqlConnection(SQLCON))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@qwid", qwid));
                cmd.Parameters.Add(GetStringSqlParameter("@id", id));
                cmd.ExecuteNonQuery();
            }
        }
        public List<YouZhaiInfo> GetYouZhaiInfos()
        {
            List<YouZhaiInfo> list = new List<YouZhaiInfo>();
            SqlConnection conn = new SqlConnection(SQLCON);
            try
            {
                string sql = $"select * from {TableName}";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        YouZhaiInfo userInfo = GetYouZhaiInfo(reader);
                        list.Add(userInfo);
                    }
                }
                return list;
            }
            finally
            {
                conn.Close();
            }
        }
        public YouZhaiInfo GetYouZhaiInfo(string qwid)
        {
            YouZhaiInfo obj = null;
            using (SqlConnection conn = new SqlConnection(SQLCON))
            {
                string sql = $"select * from {TableName} where QWID=@qwid";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@qwid", qwid));
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        obj = GetYouZhaiInfo(reader);
                    }
                }
            }
            return obj;
        }
        private YouZhaiInfo GetYouZhaiInfo(SqlDataReader reader)
        {
            YouZhaiInfo obj = new YouZhaiInfo();
            obj.id = ReadString(reader, "id");
            obj.headImg = ReadString(reader, "headImg");
            obj.nickName = ReadString(reader, "nickName");
            obj.userType = ReadString(reader, "userType");
            obj.parentId = ReadString(reader, "parentId");
            obj.deviceUniqueId = ReadString(reader, "deviceUniqueId");
            obj.deviceId = ReadString(reader, "deviceId");
            obj.deviceName = ReadString(reader, "deviceName");
            obj.remark = ReadString(reader, "remark");
            obj.orderCount = ReadIntNull(reader, "orderCount");
            obj.lastOrderTime = ReadDateTimeNull(reader, "lastOrderTime");
            obj.regTime = ReadDateTimeNull(reader, "regTime");
            obj.regChannelName = ReadString(reader, "regChannelName");
            obj.jd = ReadIntNull(reader, "jd");
            obj.tb = ReadIntNull(reader, "tb");
            obj.pdd = ReadIntNull(reader, "pdd");
            obj.dy = ReadIntNull(reader, "dy");
            obj.other = ReadIntNull(reader, "other");
            obj.yongjin = ReadDecimalNull(reader, "yongjin");
            obj.QWID = ReadString(reader, "QWID");
            return obj;
        }
    }
}
